#include "selfinfowidget.h"
#include <QCursor>

#include "debug.h"
#include "model/datacenter.h"
#include "toast.h"
#include <QTimer>
#include <QDialog>
#include <QFileDialog>

using namespace model;

SelfInfoWidget::SelfInfoWidget(QWidget* parent) :QDialog(parent)
{
    //设置窗口的属性
    this->setFixedSize(500,250);
    this->setWindowTitle("个人信息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    //当窗口被关闭的时候，将窗口销毁，不然会内存泄露
    this->setAttribute(Qt::WA_DeleteOnClose);
    //将窗口移动到当前头像的位置
    this->move(QCursor::pos());

    //创建布局管理器
    layout = new QGridLayout();
    layout->setHorizontalSpacing(10);
    layout->setVerticalSpacing(3);
    layout->setContentsMargins(20,20,20,0);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    //创建头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(75,75);
    avatarBtn->setIconSize(QSize(75,75));
    avatarBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    layout->addWidget(avatarBtn,0,0,3,1);

    QString labelStyle = "QLabel { font-size: 14px; font-weight 800; }";
    QString btnStyle = "QPushButton { border: none; background-color: transparent; }";
    btnStyle += "QPushButton:pressed { background-color: rgb(210, 210, 210); }";
    QString editStyle = "QLineEdit { border: none; border-radius: 5px; padding-left:2px; }";

    int height = 20;
    // 添加用户的id显示
    idTag = new QLabel();
    idTag->setFixedSize(50,height);
    idTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    idTag->setText("序号");
    idTag->setStyleSheet(labelStyle);

    // 编辑用户的id打高度
    idLabel = new QLabel();
    idLabel->setFixedHeight(height);
    idLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    // 添加用户的名字显示
    nameTag = new QLabel();
    nameTag->setFixedSize(50,height);
    nameTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    nameTag->setText("昵称");
    nameTag->setStyleSheet(labelStyle);

    //编辑昵称的内容的样式 高度
    nameLabel = new QLabel();
    nameLabel->setFixedHeight(height);
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    //昵称的修改按钮的大小高度设置
    nameModifyBtn = new QPushButton();
    nameModifyBtn->setFixedSize(70,25);
    nameModifyBtn->setIconSize(QSize(20,20));
    nameModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    nameModifyBtn->setStyleSheet(btnStyle);

    // 编辑昵称的高度等
    nameEdit = new QLineEdit();
    nameEdit->setFixedHeight(height);
    nameEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    nameEdit->setStyleSheet(editStyle);
    nameEdit->hide();

    //昵称的提交按钮高度等设置
    nameSubmitBtn = new QPushButton();
    nameSubmitBtn->setFixedSize(70,25);
    nameSubmitBtn->setIconSize(QSize(20,20));
    nameSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    nameSubmitBtn->setStyleSheet(btnStyle);
    nameEdit->hide();

    // 添加个性签名
    descTag = new QLabel();
    descTag->setFixedSize(50,height);
    descTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    descTag->setText("签名");
    descTag->setStyleSheet(labelStyle);

    descLabel = new QLabel();
    descLabel->setFixedHeight(height);
    descLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    descEdit = new QLineEdit();
    descEdit->setFixedHeight(height);
    descEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    descEdit->setStyleSheet(editStyle);
    descEdit->hide();

    descModifyBtn = new QPushButton();
    descModifyBtn->setFixedSize(70,25);
    descModifyBtn->setIconSize(QSize(20,20));
    descModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    descModifyBtn->setStyleSheet(btnStyle);

    descSubmitBtn = new QPushButton();
    descSubmitBtn->setFixedSize(70,25);
    descSubmitBtn->setIconSize(QSize(20,20));
    descSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    descSubmitBtn->setStyleSheet(btnStyle);
    descSubmitBtn->hide();


    // 添加电话
    phoneTag = new QLabel();
    phoneTag->setFixedSize(50,height);
    phoneTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(labelStyle);

    phoneLabel = new QLabel();
    phoneLabel->setFixedHeight(height);
    phoneLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    phoneEdit = new QLineEdit();
    phoneEdit->setFixedHeight(height);
    phoneEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    phoneEdit->setStyleSheet(editStyle);
    phoneEdit->hide();

    phoneModifyBtn = new QPushButton();
    phoneModifyBtn->setFixedSize(70,25);
    phoneModifyBtn->setIconSize(QSize(20,20));
    phoneModifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    phoneModifyBtn->setStyleSheet(btnStyle);

    phoneSubmitBtn = new QPushButton();
    phoneSubmitBtn->setFixedSize(70,25);
    phoneSubmitBtn->setIconSize(QSize(20,20));
    phoneSubmitBtn->setIcon(QIcon(":/resource/image/submit.png"));
    phoneSubmitBtn->setStyleSheet(btnStyle);
    phoneSubmitBtn->hide();

    // 添加验证码
    verifyCodeTag = new QLabel();
    verifyCodeTag->setFixedSize(50,height);
    verifyCodeTag->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    verifyCodeTag->setText("验证码");
    verifyCodeTag->setStyleSheet(labelStyle);

    verifyCodeEdit = new QLineEdit();
    verifyCodeEdit->setFixedHeight(height);
    verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    verifyCodeEdit->setStyleSheet(editStyle);
    verifyCodeEdit->hide();

    getVerifyCodeBtn = new QPushButton();
    getVerifyCodeBtn->setText("获取验证码");
    getVerifyCodeBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; } QPushButton:pressed { background-color: rgb(231, 231, 231); }");
    getVerifyCodeBtn->setFixedSize(70,height);
    getVerifyCodeBtn->hide();


    //添加到布局管理器中
    layout->addWidget(idTag,0,1);
    layout->addWidget(idLabel,0,2);

    layout->addWidget(nameTag, 1, 1);
    layout->addWidget(nameLabel, 1, 2);
    layout->addWidget(nameModifyBtn, 1, 3);

    layout->addWidget(descTag,2,1);
    layout->addWidget(descLabel,2,2);
    layout->addWidget(descModifyBtn,2,3);

    layout->addWidget(phoneTag, 3, 1);
    layout->addWidget(phoneLabel, 3, 2);
    layout->addWidget(phoneModifyBtn, 3, 3);


#if TEST_UI
    idLabel->setText("1234");
    nameLabel->setText("不摇碧莲");
    descLabel->setText("从今天开始认真敲代码");
    phoneLabel->setText("15310432984");
    avatarBtn->setIcon(QIcon(":/resource/image/avatar.png"));
#endif

    // 加载数据到界面上
    DataCenter* dataCenter = DataCenter::getInstance();
    UserInfo* userInfo = dataCenter->getMyself();
    if(userInfo != nullptr) {
        avatarBtn->setIcon(userInfo->avatar);
        nameLabel->setText(userInfo->nickname);
        descLabel->setText(userInfo->description);
        phoneLabel->setText(userInfo->phone);
        idLabel->setText(userInfo->userId);
    }

    initSignalSlot();

}

void SelfInfoWidget::initSignalSlot()
{
    connect(nameModifyBtn,&QPushButton::clicked,this,[=](){
        //将当前的nameLabel 和 nameModifyBtn 隐藏
        nameLabel->hide();
        nameModifyBtn->hide();
        layout->removeWidget(nameLabel);
        layout->removeWidget(nameModifyBtn);
        //将提交和修改框显示出来
        nameEdit->show();
        nameSubmitBtn->show();
        layout->addWidget(nameEdit,1,2);
        layout->addWidget(nameSubmitBtn,1,3);

        nameEdit->setText(nameLabel->text());
    });

    connect(descModifyBtn,&QPushButton::clicked,this,[=](){
        descLabel->hide();
        descModifyBtn->hide();
        layout->removeWidget(descLabel);
        layout->removeWidget(descModifyBtn);
        //将提交和修改框显示出来
        descEdit->show();
        descSubmitBtn->show();
        layout->addWidget(descEdit,2,2);
        layout->addWidget(descSubmitBtn,2,3);

        descEdit->setText(descLabel->text());
    });

    connect(phoneModifyBtn,&QPushButton::clicked,this,[=](){
        phoneLabel->hide();
        phoneModifyBtn->hide();
        layout->removeWidget(phoneLabel);
        layout->removeWidget(phoneModifyBtn);
        //将提交和修改框显示出来  还有验证码
        phoneEdit->show();
        phoneSubmitBtn->show();
        layout->addWidget(phoneEdit,3,2);
        layout->addWidget(phoneSubmitBtn,3,3);

        verifyCodeTag->show();
        verifyCodeEdit->show();
        getVerifyCodeBtn->show();

        layout->addWidget(verifyCodeTag, 4, 1);
        layout->addWidget(verifyCodeEdit, 4, 2);
        layout->addWidget(getVerifyCodeBtn, 4, 3);

        phoneEdit->setText(phoneLabel->text());
    });

    connect(nameSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickNameSubmitBtn);
    connect(descSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickDescSubmitBtn);
    connect(getVerifyCodeBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickGetVerifyCodeBtn);
    connect(phoneSubmitBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickPhoneSubmitBtn);
    connect(avatarBtn, &QPushButton::clicked, this, &SelfInfoWidget::clickAvatarBtn);
}

void SelfInfoWidget::clickNameSubmitBtn()
{
    // 获取输入框的内容
    const QString& nickname = nameEdit->text();
    if(nickname.isEmpty()) {
        return;
    }

    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::changeNicknameDone,this, &SelfInfoWidget::clickNameSubmitBtnDone, Qt::UniqueConnection);
    dataCenter->changeNicknameAsync(nickname);
}

void SelfInfoWidget::clickNameSubmitBtnDone()
{
    // 将编辑框改回去
    layout->removeWidget(nameEdit);;
    nameEdit->hide();
    layout->addWidget(nameLabel, 1, 2);
    nameLabel->show();
    nameLabel->setText(nameEdit->text());

    // 将提交按钮改为编辑按钮
    layout->removeWidget(nameSubmitBtn);
    nameSubmitBtn->hide();
    layout->addWidget(nameModifyBtn, 1, 3);
    nameModifyBtn->show();
}

void SelfInfoWidget::clickDescSubmitBtn()
{
    // 获取输入框的内容
    const QString& desc = descLabel->text();
    if(desc.isEmpty()) {
        return;
    }

    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::changeDescriptionDone,this, &SelfInfoWidget::chickDescSubmitBtnDone, Qt::UniqueConnection);
    dataCenter->changeDescriptionAsync(desc);
}

void SelfInfoWidget::clickGetVerifyCodeBtn()
{
    // 获取输入框内容
    const QString& phone = phoneEdit->text();
    if(phone.isEmpty()) {
        return;
    }

    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::getVerifyCodeDone,this, [=](){
        Toast::showMessage("短信验证码已经发送");
    });

    dataCenter->getVerifyCodeAsync(phone);

    // 保存手机号
    this->phoneToChange = phone;

    // 此时设置倒计时功能，并且将按钮设置为不可用
    this->getVerifyCodeBtn->setEnabled(false);

    leftTime = 30;
    QTimer* timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,[=](){
        if(leftTime <= 1) {
            getVerifyCodeBtn->setEnabled(true);
            getVerifyCodeBtn->setText("获取验证码");  
            timer->stop();
            timer->deleteLater();
            return;
        }
        --leftTime;
        getVerifyCodeBtn->setText(QString::number(leftTime) + "s");
    });

    timer->start(1000);
}

void SelfInfoWidget::clickPhoneSubmitBtn()
{
    // 判断当前验证码是否收到
    DataCenter* dataCenter = DataCenter::getInstance();
    QString verifyCodeId = dataCenter->getVerifyCodeId();
    if(verifyCodeId.isEmpty()) {
        Toast::showMessage("服务器尚未返回响应, 稍后重试!");
        return;
    }

    // 获取用户输入的验证码
    const QString& verifyCode = verifyCodeEdit->text();
    if(verifyCode.isEmpty()) {
        Toast::showMessage("验证码不能为空!");
        return;
    }

    // 获取完验证码就可以清空
    verifyCodeEdit->setText("");

    // 发送请求，进行手机号和验证码的验证
    connect(dataCenter,&DataCenter::changePhoneDone,this, &SelfInfoWidget::clickPhoneSubmitBtnDone, Qt::UniqueConnection);
    dataCenter->changePhoneAsync(this->phoneToChange,verifyCodeId,verifyCode);
}

void SelfInfoWidget::clickPhoneSubmitBtnDone()
{
    layout->removeWidget(verifyCodeTag);
    layout->removeWidget(verifyCodeEdit);
    layout->removeWidget(getVerifyCodeBtn);
    layout->removeWidget(phoneEdit);
    layout->removeWidget(phoneSubmitBtn);

    verifyCodeTag->hide();
    verifyCodeEdit->hide();
    getVerifyCodeBtn->hide();
    phoneEdit->hide();
    phoneSubmitBtn->hide();

    // 将结果添加到界面中
    phoneLabel->setText(phoneEdit->text());

    layout->addWidget(phoneLabel, 3, 2);
    phoneLabel->show();
    phoneLabel->setText(phoneToChange);
    layout->addWidget(phoneModifyBtn, 3, 3);
    phoneModifyBtn->show();
}

void SelfInfoWidget::clickAvatarBtn()
{
    // 选择要修改的图片类型
    const QString& filter = "image file (*.png *.jpg *.jpeg)";
    QString imagePath = QFileDialog::getOpenFileName(nullptr,"选择头像",QDir::homePath(),filter);

    if(imagePath.isEmpty()) {
        // 这里说明用户没有选择文件
        return;
    }

    // 加载文件
    QByteArray imageBytes  = loadFileToQByteArray(imagePath);

    // 发送网络请求，修改头像
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter,&DataCenter::changeAvatarDone,this,&SelfInfoWidget::clickAvatarBtnDone, Qt::UniqueConnection);
    dataCenter->changeAvatarAsync(imageBytes);
}

void SelfInfoWidget::clickAvatarBtnDone()
{
    // 将头像设置到界面上
    DataCenter* dataCenter = DataCenter::getInstance();
    avatarBtn->setIcon(dataCenter->getMyself()->avatar);
}

void SelfInfoWidget::chickDescSubmitBtnDone()
{
    layout->removeWidget(descEdit);
    descEdit->hide();
    layout->addWidget(descLabel,2,2);
    descLabel->show();
    descLabel->setText(descEdit->text());

    layout->removeWidget(descSubmitBtn);
    descSubmitBtn->hide();
    layout->addWidget(descModifyBtn,2,3);
    descModifyBtn->show();
}

